<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="optparse-reference-guide.html" />
<link rel="prev" href="optparse-background.html" />
<link rel="parent" href="module-optparse.html" />
<link rel="next" href="optparse-understanding-option-actions.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>14.3.2 Tutorial</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.3.1.3 what are positional"
  href="optparse-what-positional-arguments-for.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14.3 optparse  "
  href="module-optparse.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.3.2.1 understanding option actions"
  href="optparse-understanding-option-actions.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="optparse-what-positional-arguments-for.html">14.3.1.3 What are positional</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-optparse.html">14.3 optparse  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-understanding-option-actions.html">14.3.2.1 Understanding option actions</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0016320000000000000000"></a><a name="optparse-tutorial"></a>
<br>
14.3.2 Tutorial
</h2>

<p>
While <tt class="module">optparse</tt> is quite flexible and powerful, it's also straightforward to
use in most cases.  This section covers the code patterns that are
common to any <tt class="module">optparse</tt>-based program.

<p>
First, you need to import the OptionParser class; then, early in the
main program, create an OptionParser instance:
<div class="verbatim"><pre>
from optparse import OptionParser
[...]
parser = OptionParser()
</pre></div>

<p>
Then you can start defining options.  The basic syntax is:
<div class="verbatim"><pre>
parser.add_option(opt_str, ...,
                  attr=value, ...)
</pre></div>

<p>
Each option has one or more option strings, such as <code>"-f"</code> or
<code>"-file"</code>, and several option attributes that tell <tt class="module">optparse</tt> what to
expect and what to do when it encounters that option on the command
line.

<p>
Typically, each option will have one short option string and one long
option string, e.g.:
<div class="verbatim"><pre>
parser.add_option("-f", "--file", ...)
</pre></div>

<p>
You're free to define as many short option strings and as many long
option strings as you like (including zero), as long as there is at
least one option string overall.

<p>
The option strings passed to <tt class="method">add_option()</tt> are effectively labels for
the option defined by that call.  For brevity, we will frequently refer
to <em>encountering an option</em> on the command line; in reality, <tt class="module">optparse</tt>
encounters <em>option strings</em> and looks up options from them.

<p>
Once all of your options are defined, instruct <tt class="module">optparse</tt> to parse your
program's command line:
<div class="verbatim"><pre>
(options, args) = parser.parse_args()
</pre></div>

<p>
(If you like, you can pass a custom argument list to <tt class="method">parse_args()</tt>,
but that's rarely necessary: by default it uses <code>sys.argv[1:]</code>.)

<p>
<tt class="method">parse_args()</tt> returns two values:

<ul>
<li> 
<code>options</code>, an object containing values for all of your options--e.g. if <code>"-file"</code> takes a single string argument, then
<code>options.file</code> will be the filename supplied by the user, or
<code>None</code> if the user did not supply that option

<p>
</li>
<li> 
<code>args</code>, the list of positional arguments leftover after parsing
options

<p>
</li>
</ul>

<p>
This tutorial section only covers the four most important option
attributes: <tt class="member">action</tt>, <tt class="member">type</tt>, <tt class="member">dest</tt> (destination), and <tt class="member">help</tt>.
Of these, <tt class="member">action</tt> is the most fundamental.

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="optparse-understanding-option-actions.html">14.3.2.1 Understanding option actions</a>
<li><a href="optparse-store-action.html">14.3.2.2 The store action</a>
<li><a href="optparse-handling-boolean-options.html">14.3.2.3 Handling boolean (flag) options</a>
<li><a href="optparse-other-actions.html">14.3.2.4 Other actions</a>
<li><a href="optparse-default-values.html">14.3.2.5 Default values</a>
<li><a href="optparse-generating-help.html">14.3.2.6 Generating help</a>
<li><a href="optparse-printing-version-string.html">14.3.2.7 Printing a version string</a>
<li><a href="optparse-how-optparse-handles-errors.html">14.3.2.8 How <tt class="module">optparse</tt> handles errors</a>
<li><a href="optparse-putting-it-all-together.html">14.3.2.9 Putting it all together</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="14.3.1.3 what are positional"
  href="optparse-what-positional-arguments-for.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="14.3 optparse  "
  href="module-optparse.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="14.3.2.1 understanding option actions"
  href="optparse-understanding-option-actions.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="optparse-what-positional-arguments-for.html">14.3.1.3 What are positional</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-optparse.html">14.3 optparse  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-understanding-option-actions.html">14.3.2.1 Understanding option actions</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
